package com.quad.innovators.salesease.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.cache.SysRoleMenuCache;
import com.quad.innovators.salesease.constants.SecurityConstants;
import com.quad.innovators.salesease.mapper.SysRoleMenuMapper;
import com.quad.innovators.salesease.model.bo.RoleDataPermsBO;
import com.quad.innovators.salesease.model.entity.SysRoleMenu;
import com.quad.innovators.salesease.service.SysRoleMenuService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 角色菜单业务实现
 *
 * @author Ray
 * @since 2.5.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    private final SysRoleMenuCache sysRoleMenuCache;


    @PostConstruct
    public void initRolePermsCache() {
        log.info("初始化权限缓存... ");
        refreshRolePermsCache();
    }


    @Override
    public void refreshRolePermsCache() {
        // 清理权限缓存
        sysRoleMenuCache.clear();
        List<RoleDataPermsBO> sysRoleMenus = this.baseMapper.getRoleDataPermsList(null);
        if (CollectionUtil.isNotEmpty(sysRoleMenus)) {
            Map<String, List<RoleDataPermsBO>> group = sysRoleMenus.stream().collect(Collectors.groupingBy(RoleDataPermsBO::getRoleCode));
            for (Map.Entry<String, List<RoleDataPermsBO>> entry : group.entrySet()) {
                Map<String, Integer> values = new HashMap<>();
                String roleCode = entry.getKey();
                for (RoleDataPermsBO roleDataPermsBO : entry.getValue()) {
                    values.put(roleDataPermsBO.getPermsName(), roleDataPermsBO.getDataScope());
                }
                sysRoleMenuCache.set(SecurityConstants.ROLE_PERMS_PREFIX + roleCode, values);
            }
        }
    }

    @Override
    public void refreshRolePermsCache(String roleCode) {
        // 清理权限缓存
        sysRoleMenuCache.remove(SecurityConstants.ROLE_PERMS_PREFIX + roleCode);
        List<RoleDataPermsBO> sysRoleMenus = this.baseMapper.getRoleDataPermsList(Collections.singletonList(roleCode));
        if (CollectionUtil.isNotEmpty(sysRoleMenus)) {
            Map<String, List<RoleDataPermsBO>> group = sysRoleMenus.stream().collect(Collectors.groupingBy(RoleDataPermsBO::getRoleCode));
            for (Map.Entry<String, List<RoleDataPermsBO>> entry : group.entrySet()) {
                Map<String, Integer> values = new HashMap<>();
                for (RoleDataPermsBO roleDataPermsBO : entry.getValue()) {
                    values.put(roleDataPermsBO.getPermsName(), roleDataPermsBO.getDataScope());
                }
                sysRoleMenuCache.set(SecurityConstants.ROLE_PERMS_PREFIX + roleCode, values);
            }
        }
    }

    @Override
    public void refreshRolePermsCache(String oldRoleCode, String newRoleCode) {
        // 清理旧角色权限缓存
        sysRoleMenuCache.remove(SecurityConstants.ROLE_PERMS_PREFIX + oldRoleCode);
        // 添加新角色权限缓存
        refreshRolePermsCache(newRoleCode);
    }

    @Override
    public Set<String> getRolePermsByRoleCodes(Set<String> roles) {
        return this.baseMapper.listRolePerms(roles);
    }

    @Override
    public List<SysRoleMenu> getRoleMenusByRoleCodes(Set<String> roles) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>()
                .in(SysRoleMenu::getRoleCode, roles));
    }

    @Override
    public List<String> listMenuIdsByRoleId(String roleCode) {
        return this.baseMapper.listMenuIdsByRoleId(roleCode);
    }

}
